package com.clouddot.baipai.ui.Dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.clouddot.baipai.R;
import com.clouddot.baipai.http.entity.response.getLeaveTypeResponse;
import com.clouddot.baipai.infrastructure.environment.Env;
import com.clouddot.baipai.ui.adapter.leaveTypeAdapter;
import com.clouddot.baipai.util.viewUtil.getTimeAndWeatherUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by wxy on 2018/4/2.
 */

public class V_leaveDailog {
    private Dialog Dialog;
    private ConstraintLayout constraintLayout;
    private Context context;
    private EditText ed_leaveReason;
    private TextView ed_endTime, ed_startTime;
    private Spinner ed_leaveType;
    private List<String> contents = new ArrayList<String>();
    private List<getLeaveTypeResponse.LeaveTypeInfo> LeaveTypeInfolist = new ArrayList<>();
    private Button apply;
    private View view;
    private String selectIndex;
    private String selectIndexText;
    private leaveTypeAdapter leaveTypeAdapter;
    private FragmentManager manager;

    public V_leaveDailog(Context context, FragmentManager manager, List<getLeaveTypeResponse.LeaveTypeInfo> leaveTypeInfolist) {
        this.context = context;
        this.LeaveTypeInfolist = leaveTypeInfolist;
        this.manager = manager;
        initView();

    }

    public Button apply() {
        if (apply == null) {
            apply = constraintLayout.findViewById(R.id.apply);
        }
        return apply;
    }

    @SuppressLint("WrongViewCast")
    public List<String> getTextViews() {
        if (contents.isEmpty()) {
            if (TextUtils.isEmpty(ed_startTime.getText()) || TextUtils.isEmpty(ed_startTime.getText()) || TextUtils.isEmpty(ed_startTime.getText())) {
                contents.clear();
                return contents;
            }
            contents.add(selectIndex);
            contents.add(selectIndexText);
            contents.add(ed_startTime.getText().toString());
            contents.add(ed_endTime.getText().toString());
            contents.add(ed_leaveReason.getText().toString());
        }
        return contents;
    }

    private void initView() {
        constraintLayout = (ConstraintLayout) LayoutInflater.from(context).inflate(R.layout.v_dialog_leavedialog, null);
        ed_leaveType = constraintLayout.findViewById(R.id.ed_leaveType);
        ed_leaveReason = constraintLayout.findViewById(R.id.ed_leaveReason);
        ed_endTime = constraintLayout.findViewById(R.id.ed_endTime);
        ed_startTime = constraintLayout.findViewById(R.id.ed_startTime);
        leaveTypeAdapter = new leaveTypeAdapter(context);
        leaveTypeAdapter.setDatas(LeaveTypeInfolist);
        selectIndex = LeaveTypeInfolist.get(0).DimKey;
        selectIndexText = LeaveTypeInfolist.get(0).DimValue;
        ed_leaveType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                selectIndex = LeaveTypeInfolist.get(position).DimKey;
                selectIndexText = LeaveTypeInfolist.get(position).DimValue;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        ed_leaveType.setAdapter(leaveTypeAdapter);

        ed_startTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getTimeAndWeatherUtil.selectTime(manager, ed_startTime);
            }
        });

        ed_endTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getTimeAndWeatherUtil.selectTime(manager, ed_endTime);
            }
        });

    }

    public void showDialog() {

        Dialog = getDialog();
        Dialog.show();

    }

    public void closeDialog() {
        if (Dialog != null) {
            Dialog.dismiss();
        }
    }

    public Dialog getDialog() {
        if (Dialog == null) {
            Dialog = new Dialog(context, R.style.ActionDialogStyle);
        }
        Dialog.setContentView(constraintLayout);
        Window dialogWindow = Dialog.getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        dialogWindow.setAttributes(lp);
        Dialog.setCancelable(true);
        /**
         *
         * 锁入地下的虚拟按键
         */
        view = dialogWindow.getDecorView();
        lock();
        view.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int i) {
                lock();
            }
        });
        int uiFlags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE;
        view.setSystemUiVisibility(uiFlags);

        return Dialog;
    }

    private void lock() {
        int uiFlags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE;
        view.setSystemUiVisibility(uiFlags);
    }


}



